package com.klhappy.houyuan.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.klhappy.houyuan.R;
import com.klhappy.houyuan.bean.HouYuanBean.GoodsRecord;
import com.klhappy.houyuan.activity.XiangqingActivity;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.fragement.Houyuan_Fragment;
import com.klhappy.houyuan.utils.NotifyMethod;
import com.klhappy.houyuan.view.ImageManager;

public class GridViewAdapter extends BaseAdapter {

	private Context context;
	private List<GoodsRecord> goodsRecs;

	public GridViewAdapter(Context context, List<GoodsRecord> imgs) {
		super();
		this.context = context;
		this.goodsRecs = imgs;
	}

	public void setNotif(List<GoodsRecord> goodsRecs){
		this.goodsRecs = goodsRecs;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return goodsRecs.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final GoodsRecord goodsRecord = goodsRecs.get(position);
		final ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.gridview_item, null);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		ImageManager.loadImage(UrlConstant.BASE_URL + goodsRecord.getG_icon(), viewHolder.ivPhoto);
		viewHolder.tvName.setText(goodsRecord.getG_name());
		viewHolder.tvPrice.setText("￥ " + goodsRecord.getG_price());
		viewHolder.tvCount.setText(goodsRecord.getG_id() + "人已付款");
		viewHolder.ivPhoto.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, XiangqingActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable("goodId", goodsRecord.getG_id());
				intent.putExtras(bundle);
				context.startActivity(intent);
			}
		});

		viewHolder.chose_li.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String or_id = goodsRecs.get(position).getG_id();
				NotifyMethod.getInstance().postNotification(Houyuan_Fragment.AVG ,or_id);
				viewHolder.chose_li.setVisibility(View.GONE);
				viewHolder.flag = true;
			}
		});

		viewHolder.ivPhoto.setOnLongClickListener(new View.OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				if(viewHolder.flag){
					viewHolder.chose_li.setVisibility(View.VISIBLE);
					viewHolder.flag = false;
				}else {
					viewHolder.chose_li.setVisibility(View.GONE);
					viewHolder.flag = true;
				}

				return true;
			}
		});
		return convertView;
	}

	static class ViewHolder {
		@InjectView(R.id.ivPhoto) ImageView ivPhoto;
		@InjectView(R.id.tvName) TextView tvName;
		@InjectView(R.id.tvPrice) TextView tvPrice;
		@InjectView(R.id.tvCount) TextView tvCount;
		@InjectView(R.id.chose_li) LinearLayout chose_li;
		@InjectView(R.id.click_re) RelativeLayout click_re;
		private boolean flag = true;

		public ViewHolder(View view) {
		      ButterKnife.inject(this, view);
		}
	}
}
